home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Internet Info 1994 March
/
Internet Info CD-ROM (Walnut Creek) (March 1994).iso
/
networking
/
ip
/
ka9q
/
src.arc
/
FTPSUBR.C
< prev
next >
Wrap
C/C++ Source or Header
|
1989-04-16
|
2KB
|
127 lines
#include <stdio.h>
#include "global.h"
#include "mbuf.h"
#include "socket.h"
#include "ftp.h"
/* Send a file (opened by caller) on a network socket */
long
sendfile(fp,s,mode)
FILE *fp; /* File to be sent */
int s; /* Socket to be sent on */
int mode; /* Transfer mode */
{
register struct mbuf *bp;
register char *cp;
int c;
long total = 0;
switch(mode){
default:
case LOGICAL_TYPE:
case IMAGE_TYPE:
for(;;){
if((bp = alloc_mbuf(BLKSIZE)) == NULLBUF)
return -1;
if((bp->cnt = fread(bp->data,1,BLKSIZE,fp)) == 0){
free_p(bp);
break;
}
total += bp->cnt;
if(send_mbuf(s,bp,0,NULLCHAR,0) == -1)
return -1;
}
break;
case ASCII_TYPE:
for(;;){
if((bp = alloc_mbuf(BLKSIZE+1)) == NULLBUF)
return -1;
cp = bp->data;
while(bp->cnt < BLKSIZE && (c = fgetc(fp)) != EOF){
switch(c){
case '\r':
continue;
case '\n':
*cp++ = '\r';
*cp++ = '\n';
bp->cnt += 2;
break;
default:
*cp++ = c;
bp->cnt++;
break;
}
}
if(bp->cnt != 0){
total += bp->cnt;
if(send_mbuf(s,bp,0,NULLCHAR,0) == -1)
return -1;
} else {
free_p(bp);
break;
}
}
break;
}
return total;
}
long
recvfile(fp,s,mode)
FILE *fp;
int s;
int mode;
{
int cnt;
char c;
struct mbuf *bp;
long total = 0;
switch(mode){
default:
case LOGICAL_TYPE:
case IMAGE_TYPE:
while((cnt = recv_mbuf(s,&bp,0,0,NULLCHAR,0)) != 0){
if(cnt == -1)
return -1;
total += cnt;
if(write_p(fp,bp) == -1){
free_p(bp);
return -1;
}
free_p(bp);
}
break;
case ASCII_TYPE:
while((cnt = recv_mbuf(s,&bp,0,0,NULLCHAR,0)) != 0){
if(cnt == -1)
return -1;
total += cnt;
while(pullup(&bp,&c,1) == 1){
switch(uchar(c)){
case '\r':
continue;
case '\n':
#if !defined(UNIX) && !defined(__TURBOC__)
/* Needed only if the OS uses a CR/LF
* convention and fputc doesn't do
* an automatic translation
*/
fputc('\r',fp);
#endif
fputc('\n',fp);
break;
default:
if(fputc(c,fp) == EOF){
free_p(bp);
return -1;
}
}
}
}
break;
}
return total;
}